package cn.tedu.jdbc;

import java.sql.*;
import java.util.Scanner;

public class MariadbHomework6 {
    private static String username,password,nickname;
    private static int age;
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        String sql1 = "select username from userinfo where binary username = ?";
        String sql2 = "select username,password from userinfo where username = ? and password = ?";
        String sql3 = "insert into userinfo (username,password,nickname,age) values (?,?,?,?)";
        try(Connection connection = DBUtil.getConnection()){
            PreparedStatement preparedStatement1 = connection.prepareStatement(sql1);
            PreparedStatement preparedStatement2 = connection.prepareStatement(sql2);
            PreparedStatement preparedStatement3 = connection.prepareStatement(sql3);
            if (checkUsername(preparedStatement1)) {
                login(preparedStatement2);
            } else {
                register(preparedStatement3);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
    }


    public static boolean checkUsername(PreparedStatement preparedStatement){
        System.out.println("请输入用户名：");
        username = scanner.nextLine();
        try {
            preparedStatement.setString(1,username);
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                return true;
            }else{
                return false;
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return false;
    }

    public static void login(PreparedStatement preparedStatement){
        System.out.println("请输入密码：");
        password = scanner.nextLine();
        try{
            preparedStatement.setString(1,username);
            preparedStatement.setString(2,password);
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                System.out.println("登录成功！");
            }else {
                System.out.println("登录失败！");
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
    }

    public static void register(PreparedStatement preparedStatement){
        System.out.println("请输入密码：");
        password = scanner.nextLine();
        System.out.println("请输入昵称：");
        nickname = scanner.nextLine();
        System.out.println("请输入年龄：");
        age = scanner.nextInt();
        try{
            preparedStatement.setString(1,username);
            preparedStatement.setString(2,password);
            preparedStatement.setString(3,nickname);
            preparedStatement.setInt(4,age);
            if (preparedStatement.executeUpdate() > 0){
                System.out.println("注册成功！");
            }else {
                System.out.println("注册失败！");
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
    }
}
